package stark.app.function.net_ping_ip;

import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import stark.app.R;
import stark.app.databinding.ActivityBaseConvertBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.DataUtil;
import stark.common.basic.view.StkTextWatcher;

/**
 * Author：daizhongbin on 2022/8/29
 * Email：1027154338@qq.com
 * Desc:
 */
public class BaseConvertActivity extends BaseNoModelActivity<ActivityBaseConvertBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_base_convert;
    }

    @Override
    protected void initView() {
        mDataBinding.et2.addTextChangedListener(new StkTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                handleEtAfterTextChanged(mDataBinding.et2, new ICheckCharCallback() {
                    @Override
                    public boolean onCharIsNotRight(char value) {
                        return value != '0' && value != '1';
                    }
                });
            }
        });
        mDataBinding.et8.addTextChangedListener(new StkTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                handleEtAfterTextChanged(mDataBinding.et8, new ICheckCharCallback() {
                    @Override
                    public boolean onCharIsNotRight(char value) {
                        return value < '0' || value >= '8';
                    }
                });
            }
        });
        mDataBinding.et10.addTextChangedListener(new StkTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                handleEtAfterTextChanged(mDataBinding.et10, new ICheckCharCallback() {
                    @Override
                    public boolean onCharIsNotRight(char value) {
                        return value < '0' || value > '9';
                    }
                });
            }
        });
        mDataBinding.et16.addTextChangedListener(new StkTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                handleEtAfterTextChanged(mDataBinding.et16, new ICheckCharCallback() {
                    @Override
                    public boolean onCharIsNotRight(char value) {
                        return !(
                                (value >= '0' && value <= '9') ||
                                        (value >= 'A' && value <= 'F') ||
                                        (value >= 'a' && value <= 'f')
                        );
                    }
                });
            }
        });
        View.OnKeyListener keyListener = new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                int action = event.getAction();
                if (action != KeyEvent.ACTION_UP) return false;
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    //开始转换
                    if (!(v instanceof EditText)) {
                        return false;
                    }
                    String inputBase = ((EditText) v).getText().toString().trim();
                    int radix = 2;
                    if (v == mDataBinding.et8) {
                        radix = 8;
                    } else if (v == mDataBinding.et10) {
                        radix = 10;
                    } else if (v == mDataBinding.et16) {
                        radix = 16;
                    }
                    DataUtil.BaseConvertInfo convertInfo = DataUtil.convertBase(inputBase, radix);
                    mDataBinding.et2.setText(convertInfo.binary);
                    mDataBinding.et8.setText(convertInfo.octal);
                    mDataBinding.et10.setText(convertInfo.decimal);
                    mDataBinding.et16.setText(convertInfo.hex);
                }
                return false;
            }
        };
        mDataBinding.et2.setOnKeyListener(keyListener);
        mDataBinding.et8.setOnKeyListener(keyListener);
        mDataBinding.et10.setOnKeyListener(keyListener);
        mDataBinding.et16.setOnKeyListener(keyListener);

    }

    @Override
    protected void initData() {

    }

    private void handleEtAfterTextChanged(EditText editText, ICheckCharCallback callback) {
        String value = editText.getText().toString();
        if (value.length() <= 0) return;

        String newStr = getNewString(value, callback);
        if (!newStr.equals(value)) {
            editText.setText(newStr);
            editText.setSelection(newStr.length());
        }
    }

    private String getNewString(String text, ICheckCharCallback callback) {
        StringBuilder builder = new StringBuilder();
        builder.append(text);

        char val;
        for (int i = 0; i < builder.length(); i++) {
            val = builder.charAt(i);
            if (callback.onCharIsNotRight(val)) {
                builder.deleteCharAt(i);
                i--;
            }
        }
        return builder.toString();
    }

    private interface ICheckCharCallback {

        boolean onCharIsNotRight(char value);

    }
}
